package com.example.recuitactivity.candidate_fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.R;
import com.example.recuitactivity.adapter.RecruitAllInfoAdapter;
import com.example.recuitactivity.bean.RecuitInfo;

import java.util.ArrayList;
import java.util.List;


public class CandidateInfoFragment extends Fragment {
    private View messageLayout;
    private RecyclerView rv;
    private SearchView sv;
    private List<RecuitInfo> mDatas=new ArrayList<>();
    private RecruitAllInfoAdapter adapter;
    private DbOpenHelper dbOpenHelper;
    private String flag="CandidateInfoFragment";
    private String username;

    public CandidateInfoFragment(String username){
        this.username=username;
    }

    public void initRecyclerView(){
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        rv.setLayoutManager(layoutManager);
        adapter=new RecruitAllInfoAdapter(getActivity(),mDatas,flag,username);
        rv.setAdapter(adapter);
    }


    public void initUI() {
        dbOpenHelper=new DbOpenHelper(getActivity());
        rv=(RecyclerView) messageLayout.findViewById(R.id.rv_candidate_allinfo);
        sv=(SearchView) messageLayout.findViewById(R.id.sv_candidate_allinfo);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                mDatas=dbOpenHelper.getRecruitInfoByString(s);
                adapter.setmDatas(mDatas);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                mDatas=dbOpenHelper.getRecruitInfoByString(s);
                adapter.setmDatas(mDatas);
                return false;
            }
        });

    }

    public void getDatas(){
        mDatas=dbOpenHelper.getAllRecruitInfo();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.fragment_candidate_info, container, false);
        initUI();
        getDatas();
        initRecyclerView();
        return messageLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.setmDatas(mDatas);
    }
}